//
//  UIButton+KR.m
//  KEREN
//
//  Created by MW on 2023/3/14.
//

#import "UIButton+KR.h"

@implementation UIButton (KR)

+ (UIButton *)setButtonWithTitle:(NSString *)title titleColor:(UIColor *)titleColor titleFont:(UIFont *)font cornerRadius:(float)radius backColor:(UIColor *)color{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = color;
    button.layer.cornerRadius = radius;
    button.clipsToBounds = YES;
    [button setTitleColor:titleColor forState:UIControlStateNormal];
    [button setTitle:title forState:UIControlStateNormal];
    button.titleLabel.font = font;
    return button;
}
+ (UIButton *)setButtonWithImage:(NSString *)imageName  cornerRadius:(float)radius backColor:(UIColor *)color{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    if(radius > 0){
        button.layer.cornerRadius = radius;
        button.clipsToBounds = YES;
    }
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    if([color isEqual:[UIColor clearColor]]){
        button.backgroundColor = color;
    }
    return button;

}

+ (UIButton *)setButtonWithImage:(NSString *)imageName withleftTitle:(NSString *)title inSuperView:(UIView *)superView{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [superView addSubview:button];
    button.frame = CGRectMake(0, 0, 110, 28);
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [button setTitle:title forState:UIControlStateNormal];
    button.titleLabel.font = BFont(20);
    [button setTitleColor:Black_Color forState:UIControlStateNormal];
    CGFloat labelWidth = button.titleLabel.bounds.size.width;
        // 取出 imageView 的宽度
    CGFloat imageWidth = button.imageView.bounds.size.width;
        // 设置 titleLabel 的内边距
    button.titleEdgeInsets = UIEdgeInsetsMake(0, -imageWidth, 0, imageWidth);
        // 设置 imageView 的内边距
    button.imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth + 15, 0, -labelWidth - 15);
    return button;
}
@end
